﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EDTS.Model.Weixin;
using EDTS.Model.Weixin.Messages;
using EDTS.Logic.Orgnizations;
using EDTS.Logic.Users;
namespace EDTS.Logic.Weixin.Messages
{
    public class SystemTransmit
    {
        /// <summary>
        /// 将一条文本消息加入发送列表
        /// </summary>
        /// <param name="unitGuid"></param>
        /// <param name="receiverGuid"></param>
        /// <param name="content"></param>
        /// <param name="sendTime"></param>
        /// <param name="safe"></param>
        public static void Text(Guid unitGuid, Guid receiverGuid, string content,DateTime sendTime,bool safe=false)
        {
            var unit = TransmitCheck.Unit(unitGuid);
            var user = TransmitCheck.Receiver(receiverGuid);

            if (string.IsNullOrEmpty(content))
                throw new Utils.IException("无内容");
            var model = new WxMessageModel()
            {
                AppGuid = unit.WxApp.Guid,
                Receiver = user.UserName,
                MsgType = "Text",
                Description = content,
                SendTime = sendTime,
                Safe = safe
            };
            WxMessageLogic.CreateNew(model);
        }
        /// <summary>
        /// 将一条图文消息加入发送列表
        /// </summary>
        /// <param name="unitGuid"></param>
        /// <param name="receiverGuid"></param>
        /// <param name="articles"></param>
        /// <param name="sendTime"></param>
        public static void News(Guid unitGuid, Guid receiverGuid, List<Article> articles, DateTime sendTime)
        {
            var unit = TransmitCheck.Unit(unitGuid);
            var user = TransmitCheck.Receiver(receiverGuid);

            if (articles == null || articles.Count == 0)
                throw new Utils.IException("无内容");
            var model = new WxMessageModel()
            {
                AppGuid = unit.WxApp.Guid,
                Receiver = user.UserName,
                MsgType = "News",
                SendTime = sendTime,
                Description = Newtonsoft.Json.JsonConvert.SerializeObject(articles)
            };
            WxMessageLogic.CreateNew(model);
        }
    }
}
